//
//  FaceGatherViewController.m
//  Rail
//
//  Created by hychen on 2018/3/3.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "FaceGatherViewController.h"
#import "LineDetailDescTableViewCell.h"
#import "SelectPhotoTableViewCell.h"
#import "ButtonTableViewCell.h"

#import "SelectUserViewController.h"

@interface FaceGatherViewController ()
@property (nonatomic, strong) NSMutableArray *selectPhotoArray;

@property (nonatomic, assign) BOOL isUploading;
@end

@implementation FaceGatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem addSimpleTitleView:@"采集人脸"];
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LineDetailDescTableViewCell *cell = [LineDetailDescTableViewCell cellWithTableView:tableView class:[LineDetailDescTableViewCell class]];
        cell.titleLabel.text = @"采集用户";
        cell.contentLabel.text = self.userModel.username?:@"请选择";
        if (!([self.userModel.username isKindOfClass:[NSString class]] && self.userModel.username.length > 0)) {
            cell.isPlaceholder = YES;
        }
        else {
            cell.isPlaceholder = NO;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row == 1) {
        SelectPhotoTableViewCell *cell = [SelectPhotoTableViewCell cellWithInitStyleTableView:tableView class:[SelectPhotoTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setTitle:@"人脸照片"];
        [cell setPhotoArray:self.selectPhotoArray];
        cell.maxCount = 3;
        BMWeakSelf
        [cell setBlockSelectPhotoArray:^(NSArray *photoArray) {
            weakSelf.selectPhotoArray = [NSMutableArray arrayWithArray:photoArray];
        }];
        return cell;
    }
    if (indexPath.row == 2) {
        ButtonTableViewCell *cell = [ButtonTableViewCell cellWithTableView:tableView class:[ButtonTableViewCell class]];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.buttonTitle = @"保存";
        BMWeakSelf
        [cell setBlockClickEventButton:^{
            [weakSelf requestFaceGather];
        }];
        
        return cell;
    }
    return [UITableViewCell new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 50.f;
    }
    if (indexPath.row == 1) {
        return 160.f;
    }
    if (indexPath.row == 2) {
        return 200.f;
    }
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)requestFaceGather {
    
    if (self.isUploading) {
        return;
    }
    
    if (!self.userModel) {
        Show_Msg_Error(@"请选择采集用户")
        return;
    }
    
    if (self.selectPhotoArray.count < 1) {
        Show_Msg_Error(@"请选择图片")
        return;
    }
    
    BMWeakSelf
    if (![BMUtils checkOpen4gAutoUploadWithCompletion:^{
        [weakSelf _requestFaceGather];
    }]) {
        return;
    }
    
    [self _requestFaceGather];
}

- (void)_requestFaceGather {
    BMWeakSelf
    
    UIImage *image1 = nil;
    if (self.selectPhotoArray.count > 0) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:0];
        image1 = [imageModel uploadImage]?:[imageModel image];
    }
    
    UIImage *image2 = nil;
    if (self.selectPhotoArray.count > 1) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:1];
        image2 = [imageModel uploadImage]?:[imageModel image];
    }
    
    UIImage *image3 = nil;
    if (self.selectPhotoArray.count > 2) {
        SelectPhotoModel *imageModel = [self.selectPhotoArray objectAtIndex:2];
        image3 = [imageModel uploadImage]?:[imageModel image];
    }
    
    self.isUploading = YES;
    
    Show_Msg(@"采集中")
    
    [HttpRequest uploadGatherFaceUserDataWithUserId:self.userModel.user_account type:self.userModel.type visitorName:nil image1:image1 image2:image2 image3:image3 success:^(id object) {
        weakSelf.isUploading = NO;
        Show_Msg_Success(@"采集成功")
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (weakSelf.BlockGatherFaceSuccess) {
                weakSelf.BlockGatherFaceSuccess();
            }
            Dismiss_NoMsg
            if (weakSelf.isFromRegather && weakSelf.navigationController.viewControllers.count > 2) {
                NSMutableArray *array = [NSMutableArray arrayWithArray:weakSelf.navigationController.viewControllers];
                [array removeObjectAtIndex:weakSelf.navigationController.viewControllers.count-2];
                weakSelf.navigationController.viewControllers = array.copy;
            }
            
            [weakSelf.navigationController popViewControllerAnimated:YES];
            
        });
    } failure:^(NSString *code, NSString *msg) {
        weakSelf.isUploading = NO;
        Show_Msg_Error(msg)
    }];
    
}

- (NSMutableArray *)selectPhotoArray {
    if (!_selectPhotoArray) {
        _selectPhotoArray = [NSMutableArray array];
    }
    return _selectPhotoArray;
}

@end
